<?php echo $header; ?>
<div id="content">
  <div class="breadcrumb">
    <?php foreach ($breadcrumbs as $breadcrumb) { ?>
    <?php echo $breadcrumb['separator']; ?><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a>
    <?php } ?>
  </div>
  <?php if ($error_warning) { ?>
  <div class="warning"><?php echo $error_warning; ?></div>
  <?php } ?>
  <div class="box">
    <div class="heading">
      <h1><img src="view/image/order.png" alt="" /> <?php echo $heading_title; ?></h1>
      <div class="buttons"><a onclick="speedySubmit();" class="button"><?php echo $button_create; ?></a><a href="<?php echo $cancel; ?>" class="button"><?php echo $button_cancel; ?></a></div>
    </div>
    <div class="content">
      <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form">
        <input id="speedy_taking_date" type="hidden" name="taking_date" value="<?php echo $taking_date; ?>" />
        <table class="form">
          <tr>
            <td><span class="required">*</span> <label for="speedy_contents"><?php echo $entry_contents; ?></label></td>
            <td><input type="text" id="speedy_contents" name="contents" value="<?php echo $contents; ?>" />
              <?php if ($error_contents) { ?>
              <span class="error"><?php echo $error_contents; ?></span>
              <?php } ?></td>
          </tr>
          <tr>
            <td><span class="required">*</span> <label for="speedy_weight"><?php echo $entry_weight; ?></label></td>
            <td><input type="text" id="speedy_weight" name="weight" value="<?php echo $weight; ?>" />
              <?php if ($error_weight) { ?>
              <span class="error"><?php echo $error_weight; ?></span>
              <?php } ?></td>
          </tr>
          <tr>
            <td><span class="required">*</span> <label for="speedy_packing"><?php echo $entry_packing; ?></label></td>
            <td><input type="text" id="speedy_packing" name="packing" value="<?php echo $packing; ?>" />
              <?php if ($error_packing) { ?>
              <span class="error"><?php echo $error_packing; ?></span>
              <?php } ?></td>
          </tr>
          <tr>
            <td><span class="required">*</span> <label for="speedy_count"><?php echo $entry_count; ?></label></td>
            <td><input type="text" id="speedy_count" name="count" value="<?php echo $count; ?>" />
              <?php if ($error_count) { ?>
              <span class="error"><?php echo $error_count; ?></span>
              <?php } ?></td>
          </tr>
          <tr>
            <td><label for="speedy_width"><?php echo $entry_size; ?></label></td>
            <td><input type="text" id="speedy_width" name="width" value="<?php echo $width; ?>" size="3" /> x
              <input type="text" name="height" value="<?php echo $height; ?>" size="3" /> x
              <input type="text" name="depth" value="<?php echo $depth; ?>" size="3" /></td>
          </tr>
          <tr>
            <td><label for="speedy_deffered_days"><?php echo $entry_deffered_days; ?></label></td>
            <td><select id="speedy_deffered_days" name="deffered_days">
                <?php foreach ($days as $day_id => $day) { ?>
                <?php if ($day_id == $deffered_days) { ?>
                <option value="<?php echo $day_id; ?>" selected="selected"><?php echo $day; ?></option>
                <?php } else { ?>
                <option value="<?php echo $day_id; ?>"><?php echo $day; ?></option>
                <?php } ?>
                <?php } ?>
              </select></td>
          </tr>
          <tr>
            <td><label for="speedy_client_note"><?php echo $entry_client_note; ?></label></td>
            <td><input type="text" id="speedy_client_note" name="client_note" value="<?php echo $client_note; ?>" size="50" /></td>
          </tr>
          <tr>
            <td><?php echo $entry_cod; ?></td>
            <td><input type="radio" id="speedy_cod_yes" name="cod" value="1" <?php if ($cod) { ?> checked="checked"<?php } ?> onclick="$(this).parent().parent().next().show();" />
              <label for="speedy_cod_yes"><?php echo $text_yes; ?></label>
              <input type="radio" id="speedy_cod_no" name="cod" value="0" <?php if (!$cod) { ?> checked="checked"<?php } ?> onclick="$(this).parent().parent().next().hide();" />
              <label for="speedy_cod_no"><?php echo $text_no; ?></label></td>
          </tr>
          <tr <?php if (!$cod) { ?> style="display: none;"<?php } ?>>
            <td><label for="speedy_total"><?php echo $entry_total; ?></label></td>
            <td><input type="text" id="speedy_total" name="total" value="<?php echo $total; ?>" /></td>
          </tr>
          <tr>
            <td><label for="speedy_insurance"><?php echo $entry_insurance; ?></label></td>
            <td><select id="speedy_insurance" name="insurance" onchange="$('#speedy_fragile').parent().parent().toggle(); $('#speedy_total_insurance').parent().parent().toggle();">
                <?php if ($insurance) { ?>
                <option value="1" selected="selected"><?php echo $text_yes; ?></option>
                <option value="0"><?php echo $text_no; ?></option>
                <?php } else { ?>
                <option value="1"><?php echo $text_yes; ?></option>
                <option value="0" selected="selected"><?php echo $text_no; ?></option>
                <?php } ?>
              </select></td>
          </tr>
          <tr <?php if (!$insurance) { ?> style="display: none;"<?php } ?>>
            <td><label for="speedy_fragile"><?php echo $entry_fragile; ?></label></td>
            <td><select id="speedy_fragile" name="fragile">
                <?php if ($fragile) { ?>
                <option value="1" selected="selected"><?php echo $text_yes; ?></option>
                <option value="0"><?php echo $text_no; ?></option>
                <?php } else { ?>
                <option value="1"><?php echo $text_yes; ?></option>
                <option value="0" selected="selected"><?php echo $text_no; ?></option>
                <?php } ?>
              </select></td>
          </tr>
          <tr <?php if (!$insurance) { ?> style="display: none;"<?php } ?>>
            <td><label for="speedy_total_insurance"><?php echo $entry_total_insurance; ?></label></td>
            <td><input type="text" id="speedy_total_insurance" name="totalNoShipping" value="<?php echo $totalNoShipping; ?>" /></td>
          </tr>
          <tr>
            <td><?php echo $entry_shipping_to; ?></td>
            <td><input type="radio" id="speedy_shipping_to_door" name="to_office" value="0" <?php if (!$to_office) { ?> checked="checked"<?php } ?> onclick="$('#speedy_quarter_container,#speedy_street_container,#speedy_block_no_container,#speedy_note_container').show(); $('#speedy_office_container').hide();" />
              <label for="speedy_shipping_to_door"><?php echo $text_to_door; ?></label>
              <input type="radio" id="speedy_shipping_to_office" name="to_office" value="1" <?php if ($to_office) { ?> checked="checked"<?php } ?> onclick="$('#speedy_quarter_container,#speedy_street_container,#speedy_block_no_container,#speedy_note_container').hide(); $('#speedy_office_container').show();" />
              <label for="speedy_shipping_to_office"><?php echo $text_to_office; ?></label></td>
          </tr>
          <tr>
            <td><label for="speedy_city"><?php echo $entry_city; ?></label></td>
            <td><input type="text" id="speedy_city" name="city" value="<?php echo $city; ?>" size="39" />
              <input type="hidden" id="speedy_city_id" name="city_id" value="<?php echo $city_id; ?>" />
              <input type="hidden" id="speedy_city_nomenclature" name="city_nomenclature" value="<?php echo $city_nomenclature; ?>" />
              <label for="speedy_postcode"><?php echo $entry_postcode; ?></label>
              <input type="text" id="speedy_postcode" name="postcode" value="<?php echo $postcode; ?>" disabled="disabled" size="3" /></td>
          </tr>
          <tr id="speedy_quarter_container" <?php if ($to_office) { ?> style="display: none;"<?php } ?>>
            <td><label for="speedy_quarter"><?php echo $entry_quarter; ?></label></td>
            <td><input type="text" id="speedy_quarter" name="quarter" value="<?php echo $quarter; ?>" size="52" />
              <input type="hidden" id="speedy_quarter_id" name="quarter_id" value="<?php echo $quarter_id; ?>" /></td>
          </tr>
          <tr id="speedy_street_container" <?php if ($to_office) { ?> style="display: none;"<?php } ?>>
            <td><label for="speedy_street"><?php echo $entry_street; ?></label></td>
            <td><input type="text" id="speedy_street" name="street" value="<?php echo $street; ?>" size="39" />
              <input type="hidden" id="speedy_street_id" name="street_id" value="<?php echo $street_id; ?>" />
              <label for="speedy_street_no"><?php echo $entry_street_no; ?></label>
              <input type="text" id="speedy_street_no" name="street_no" value="<?php echo $street_no; ?>" size="3" /></td>
          </tr>
          <tr id="speedy_block_no_container" <?php if ($to_office) { ?> style="display: none;"<?php } ?>>
            <td><label for="speedy_block_no"><?php echo $entry_block_no; ?></label></td>
            <td><input type="text" id="speedy_block_no" name="block_no" value="<?php echo $block_no; ?>" size="12" />
              <label for="speedy_entrance_no"><?php echo $entry_entrance_no; ?></label>
              <input type="text" id="speedy_entrance_no" name="entrance_no" value="<?php echo $entrance_no; ?>" size="3" />
              <label for="speedy_floor_no"><?php echo $entry_floor_no; ?></label>
              <input type="text" id="speedy_floor_no" name="floor_no" value="<?php echo $floor_no; ?>" size="3" />
              <label for="speedy_apartment_no"><?php echo $entry_apartment_no; ?></label>
              <input type="text" id="speedy_apartment_no" name="apartment_no" value="<?php echo $apartment_no; ?>" size="3" />
              <?php if ($error_address) { ?>
              <span class="error"><?php echo $error_address; ?></span>
              <?php } ?></td>
          </tr>
          <tr id="speedy_note_container" <?php if ($to_office) { ?> style="display: none;"<?php } ?>>
            <td><label for="speedy_note"><?php echo $entry_note; ?></label></td>
            <td><input type="text" id="speedy_note" name="note" value="<?php echo $note; ?>" size="52" /></td>
          </tr>
          <tr id="speedy_office_container" <?php if (!$to_office) { ?> style="display: none;"<?php } ?>>
            <td><label for="speedy_office_id"><?php echo $entry_office; ?></label></td>
            <td><select id="speedy_office_id" name="office_id" style="width: 400px;">
                <?php if (!$offices) { ?><option value="0" selected="selected"><?php echo $text_select_city; ?></option><?php } ?>
                <?php foreach ($offices as $office) { ?>
                <?php if ($office['id'] == $office_id) { ?>
                <option value="<?php echo $office['id']; ?>" selected="selected"><?php echo $office['label']; ?></option>
                <?php } else { ?>
                <option value="<?php echo $office['id']; ?>"><?php echo $office['label']; ?></option>
                <?php } ?>
                <?php } ?>
              </select>
              <?php if ($error_office) { ?>
              <span class="error"><?php echo $error_office; ?></span>
              <?php } ?></td>
          </tr>
          <tr <?php if (!$fixed_time) { ?> style="display: none;"<?php } ?>>
            <td><input type="checkbox" id="speedy_fixed_time_cb" name="fixed_time_cb" value="1" <?php if ($fixed_time_cb) { ?> checked="checked"<?php } ?> onclick="speedyCheckFixedTime();" />
              <label for="speedy_fixed_time_cb"><?php echo $entry_fixed_time; ?></label></td>
            <td><select id="speedy_fixed_time_hour" name="fixed_time_hour" <?php if (!$fixed_time_cb) { ?> disabled="disabled"<?php } ?> onchange="speedySetFixedTime();">
                <?php for ($i = 10; $i <= 17; $i++) { ?>
                <?php $hour = str_pad($i, 2, '0', STR_PAD_LEFT); ?>
                <?php if ($hour == $fixed_time_hour || !$fixed_time_hour) { ?>
                <?php $fixed_time_hour = $hour; ?>
                <option value="<?php echo $hour; ?>" selected="selected"><?php echo $hour; ?></option>
                <?php } else { ?>
                <option value="<?php echo $hour; ?>"><?php echo $hour; ?></option>
                <?php } ?>
                <?php } ?>
              </select>
              <select id="speedy_fixed_time_min" name="fixed_time_min" <?php if (!$fixed_time_cb) { ?> disabled="disabled"<?php } ?>>
                <?php $min_fixed_time_mins = ($fixed_time_hour == 10 ? 30 : 0); ?>
                <?php $max_fixed_time_mins = ($fixed_time_hour == 17 ? 30 : 59); ?>
                <?php for ($i = $min_fixed_time_mins; $i <= $max_fixed_time_mins; $i++) { ?>
                <?php $hour = str_pad($i, 2, '0', STR_PAD_LEFT); ?>
                <?php if ($hour == $fixed_time_min) { ?>
                <option value="<?php echo $hour; ?>" selected="selected"><?php echo $hour; ?></option>
                <?php } else { ?>
                <option value="<?php echo $hour; ?>"><?php echo $hour; ?></option>
                <?php } ?>
                <?php } ?>
              </select>
              <?php echo $text_fixed_time; ?>
              <?php if ($error_fixed_time) { ?>
              <span class="error"><?php echo $error_fixed_time; ?></span>
              <?php } ?></td>
          </tr>
          <?php if (!empty($quote)) { ?>
          <tr>
            <td><?php echo $entry_shipping_method; ?></td>
            <td>
              <?php foreach ($quote['quote'] as $shipping_method_key_id => $shipping_method) { ?>
              <input type="radio" id="<?php echo $shipping_method['code']; ?>" name="shipping_method" value="<?php echo $shipping_method['code']; ?>" <?php if ($shipping_method_key_id == $shipping_method_id) { ?> checked="checked"<?php } ?> />
              <label for="<?php echo $shipping_method['code']; ?>"><?php echo $shipping_method['title']; ?></label>
              <label for="<?php echo $shipping_method['code']; ?>"><?php echo $shipping_method['text']; ?></label><br />
              <?php } ?></td>
          </tr>
          <?php } ?>
          <tr>
            <td><b><?php echo $text_calculate; ?></b></td>
            <td><input type="hidden" id="calculate" name="calculate" value="0" /><a onclick="$('#calculate').val('1'); $('#form :input').removeAttr('disabled'); $('#form').submit();" class="button"><?php echo $button_calculate; ?></a></td>
          </tr>
        </table>
      </form>
    </div>
  </div>
</div>
<script type="text/javascript"><!--
function speedySubmit() {
	$.ajax({
		url: 'index.php?route=sale/speedy/checkDate&token=<?php echo $token; ?>',
		type: 'POST',
		data: 'shipping_method_id=' + encodeURIComponent('<?php echo $shipping_method_id; ?>'),
		dataType: 'json',
		success: function(data) {
			if (data.error) {
				if (data.taking_date) {
					$('#speedy_taking_date').val(data.taking_date);

					if (confirm(data.warning)) {
						$('#form :input').removeAttr('disabled');
						$('#form').submit();
					}
				} else if (data.warning) {
					alert(data.warning);
				}
			} else {
				$('#form :input').removeAttr('disabled');
				$('#form').submit();
			}
		},
		error: function(xhr, ajaxOptions, thrownError) {
			//alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});
}

function speedyCheckFixedTime() {
	if ($('#speedy_fixed_time_cb:checked').length) {
		$('#speedy_fixed_time_hour').removeAttr('disabled');
		$('#speedy_fixed_time_min').removeAttr('disabled');
	} else {
		$('#speedy_fixed_time_hour').attr('disabled', 'disabled');
		$('#speedy_fixed_time_min').attr('disabled', 'disabled');
	}
}

function speedySetFixedTime() {
	if ($('#speedy_fixed_time_hour').val() == 10) {
		min_fixed_time_mins = 30;
	} else {
		min_fixed_time_mins = 0;
	}

	if ($('#speedy_fixed_time_hour').val() == 17) {
		max_fixed_time_mins = 30;
	} else {
		max_fixed_time_mins = 59;
	}

	html = '';

	for (i = min_fixed_time_mins; i <= max_fixed_time_mins; i++) {
		iStr = i.toString();

		if (iStr.length < 2) {
			fixed_time_min = '0' + i;
		} else {
			fixed_time_min = i;
		}

		html += '<option value="' + fixed_time_min + '">' + fixed_time_min + '</option>';
	}

	$('#speedy_fixed_time_min').html(html);
}

function speedy(speedy_id) {
	speedy_has_fixed_time = false;

<?php if (isset($this->session->data['loading_methods_fixed_time'])) { ?>
	speedy_methods_fixed_time = new Array("<?php echo implode('", "', array_keys($this->session->data['loading_methods_fixed_time'])); ?>");

	for (speedy_method in speedy_methods_fixed_time) {
		if (('speedy.' + speedy_methods_fixed_time[speedy_method]) == speedy_id) {
			speedy_has_fixed_time = true;
			break;
		}
	}
<?php } ?>

	if (speedy_has_fixed_time) {
		$('#speedy_fixed_time_cb').removeAttr('disabled');
	} else {
		$('#speedy_fixed_time_cb').attr('disabled', 'disabled');
		$('#speedy_fixed_time_cb').attr('checked', false);
		$('#speedy_fixed_time_hour').attr('disabled', 'disabled');
		$('#speedy_fixed_time_min').attr('disabled', 'disabled');
	}
}

var speedy_city = '<?php echo $city; ?>';
var speedy_quarter = '<?php echo $quarter; ?>';
var speedy_street = '<?php echo $street; ?>';

$(document).ready(function() {
	if ($('[name="shipping_method"][id^="speedy."]:checked').length) {
		speedy($('[name="shipping_method"][id^="speedy."]:checked').attr('id'));
	} else if ($('[name="shipping_method"][id^="speedy."]').length) {
		$('#speedy_fixed_time_cb').attr('disabled', 'disabled');
		$('#speedy_fixed_time_cb').attr('checked', false);
		$('#speedy_fixed_time_hour').attr('disabled', 'disabled');
		$('#speedy_fixed_time_min').attr('disabled', 'disabled');
	}

	$('[name="shipping_method"][id^="speedy."]').click(function() {
		speedy($(this).attr('id'));
	});

	$('#speedy_city').autocomplete({
		source: function(request, response) {
			var $this = $(this);
			var $element = $(this.element);
			var jqXHR = $element.data('jqXHR');
			if (jqXHR) {
				jqXHR.abort();
			}
			$element.data('jqXHR', $.ajax({
				url: 'index.php?route=sale/speedy/getCities&token=<?php echo $token; ?>',
				dataType: 'json',
				data: {
					term: request.term
				},
				complete: function() {
					$this.removeData('jqXHR');
				},
				success: function(data) {
					if (data.error) {
						alert(data.error);
					} else {
						if (data.length) {
							response(data);
						}
					}
				},
				error: function(xhr, ajaxOptions, thrownError) {
					//alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
				}
			}));
		},
		minLength: 1,
		select: function(event, ui) {
			if (ui.item) {
				speedy_city = ui.item.value;
				$('#speedy_postcode').val(ui.item.postcode);
				$('#speedy_city_id').val(ui.item.id);
				$('#speedy_city_nomenclature').val(ui.item.nomenclature);
				$('#speedy_quarter').val('');
				$('#speedy_quarter_id').val('');
				$('#speedy_street').val('');
				$('#speedy_street_id').val('');
				$('#speedy_street_no').val('');
				$('#speedy_block_no').val('');
				$('#speedy_entrance_no').val('');
				$('#speedy_floor_no').val('');
				$('#speedy_apartment_no').val('');
				$('#speedy_note').val('');
				$('#speedy_office_id').html('<option value="0"><?php echo $text_wait; ?></option>');
/*
				if (ui.item.nomenclature == 'NO') {
					$('#speedy_quarter, #speedy_street').autocomplete('option', 'disabled', true);
				} else {
					$('#speedy_quarter, #speedy_street').autocomplete('option', 'disabled', false);
				}
*/
				$.ajax({
					url: 'index.php?route=sale/speedy/getOffices&token=<?php echo $token; ?>',
					dataType: 'json',
					data: {
						city_id: ui.item.id
					},
					success: function(data) {
						if (data.error) {
							alert(data.error);
						} else {
							html = '';

							if (data.length) {
								for (i = 0; i < data.length; i++) {
									html += '<option value="' + data[i]['id'] + '">' + data[i]['label'] + '</option>';
								}
							} else {
								html += '<option value="0"><?php echo $text_select_city; ?></option>';
							}

							$('#speedy_office_id').html(html);
						}
					},
					error: function(xhr, ajaxOptions, thrownError) {
						//alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
					}
				});
			}
		},
		change: function(event, ui) {
			if(!ui.item) {
				$('#speedy_city').val('');
				$('#speedy_city_id').val('');
				$('#speedy_city_nomenclature').val('');
				$('#speedy_postcode').val('');
				$('#speedy_office_id').html('<option value="0"><?php echo $text_select_city; ?></option>');
			}

			$('#speedy_quarter').val('');
			$('#speedy_quarter_id').val('');
			$('#speedy_street').val('');
			$('#speedy_street_id').val('');
			$('#speedy_street_no').val('');
			$('#speedy_block_no').val('');
			$('#speedy_entrance_no').val('');
			$('#speedy_floor_no').val('');
			$('#speedy_apartment_no').val('');
			$('#speedy_note').val('');
		}
	});

	$('#speedy_city').blur(function() {
		var $this = $(this);
		var jqXHR = $(this).data('jqXHR');
		if (jqXHR) {
			jqXHR.abort();
		}
		$this.removeData('jqXHR');

		if ($this.val() != speedy_city) {
			$('#speedy_city').val('');
			$('#speedy_city_id').val('');
			$('#speedy_city_nomenclature').val('');
			$('#speedy_postcode').val('');
			$('#speedy_office_id').html('<option value="0"><?php echo $text_select_city; ?></option>');
			$('#speedy_quarter').val('');
			$('#speedy_quarter_id').val('');
			$('#speedy_street').val('');
			$('#speedy_street_id').val('');
			$('#speedy_street_no').val('');
			$('#speedy_block_no').val('');
			$('#speedy_entrance_no').val('');
			$('#speedy_floor_no').val('');
			$('#speedy_apartment_no').val('');
			$('#speedy_note').val('');
		}
	});

	$('#speedy_quarter').autocomplete({
		source: function(request, response) {
			var $this = $(this);
			var $element = $(this.element);
			var jqXHR = $element.data('jqXHR');
			if (jqXHR) {
				jqXHR.abort();
			}
			$element.data('jqXHR', $.ajax({
				url: 'index.php?route=sale/speedy/getQuarters&token=<?php echo $token; ?>',
				dataType: 'json',
				data: {
					term: request.term,
					city_id: function() { return $('#speedy_city_id').val(); }
				},
				complete: function() {
					$this.removeData('jqXHR');
				},
				success: function(data) {
					if (data.error) {
						$('#speedy_quarter').val('');
						$('#speedy_quarter_id').val('');
						alert(data.error);
					} else {
						if ($('#speedy_city_nomenclature').val() == 'FULL') {
							if (data.length) {
								response(data);
							}
						} else {
							response(data);
						}
					}
				},
				error: function(xhr, ajaxOptions, thrownError) {
					//alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
				}
			}));
		},
		minLength: 1,
		select: function(event, ui) {
			if (ui.item) {
				speedy_quarter = ui.item.value;
				$('#speedy_quarter_id').val(ui.item.id);
			}
		},
		change: function(event, ui) {
			if(!ui.item && $('#speedy_city_nomenclature').val() == 'FULL') {
				$('#speedy_quarter').val('');
				$('#speedy_quarter_id').val('');
			}
		}
	});

	$('#speedy_quarter').blur(function() {
		var $this = $(this);
		var jqXHR = $(this).data('jqXHR');
		if (jqXHR) {
			jqXHR.abort();
		}
		$this.removeData('jqXHR');

		if (($this.val() != speedy_quarter) && ($('#speedy_city_nomenclature').val() == 'FULL')) {
			$('#speedy_quarter').val('');
			$('#speedy_quarter_id').val('');
		}
	});

	$('#speedy_street').autocomplete({
		source: function(request, response) {
			var $this = $(this);
			var $element = $(this.element);
			var jqXHR = $element.data('jqXHR');
			if (jqXHR) {
				jqXHR.abort();
			}
			$element.data('jqXHR', $.ajax({
				url: 'index.php?route=sale/speedy/getStreets&token=<?php echo $token; ?>',
				dataType: 'json',
				data: {
					term: request.term,
					city_id: function() { return $('#speedy_city_id').val(); }
				},
				complete: function() {
					$this.removeData('jqXHR');
				},
				success: function(data) {
					if (data.error) {
						$('#speedy_street').val('');
						$('#speedy_street_id').val('');
						alert(data.error);
					} else {
						if ($('#speedy_city_nomenclature').val() == 'FULL') {
							if (data.length) {
								response(data);
							}
						} else {
							response(data);
						}
					}
				},
				error: function(xhr, ajaxOptions, thrownError) {
					//alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
				}
			}));
		},
		minLength: 1,
		select: function(event, ui) {
			if (ui.item) {
				speedy_street = ui.item.value;
				$('#speedy_street_id').val(ui.item.id);
			}
		},
		change: function(event, ui) {
			if(!ui.item && $('#speedy_city_nomenclature').val() == 'FULL') {
				$('#speedy_street').val('');
				$('#speedy_street_id').val('');
			}
		}
	});

	$('#speedy_street').blur(function() {
		var $this = $(this);
		var jqXHR = $(this).data('jqXHR');
		if (jqXHR) {
			jqXHR.abort();
		}
		$this.removeData('jqXHR');

		if (($this.val() != speedy_street) && ($('#speedy_city_nomenclature').val() == 'FULL')) {
			$('#speedy_street').val('');
			$('#speedy_street_id').val('');
		}
	});

	$('#speedy_block_no').autocomplete({
		source: function(request, response) {
			var $this = $(this);
			var $element = $(this.element);
			var jqXHR = $element.data('jqXHR');
			if (jqXHR) {
				jqXHR.abort();
			}
			$element.data('jqXHR', $.ajax({
				url: 'index.php?route=sale/speedy/getBlocks&token=<?php echo $token; ?>',
				dataType: 'json',
				data: {
					term: request.term,
					city_id: function() { return $('#speedy_city_id').val(); }
				},
				complete: function() {
					$this.removeData('jqXHR');
				},
				success: function(data) {
					if (data.error) {
						$('#speedy_block_no').val('');
						alert(data.error);
					} else {
						response(data);
					}
				},
				error: function(xhr, ajaxOptions, thrownError) {
					//alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
				}
			}));
		},
		minLength: 1
	});

	$('#speedy_block_no').blur(function() {
		var $this = $(this);
		var jqXHR = $(this).data('jqXHR');
		if (jqXHR) {
			jqXHR.abort();
		}
		$this.removeData('jqXHR');
	});
});
//--></script>
<?php echo $footer; ?>